package com.star.starp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.star.starp.config.SystemConfig;
import com.star.starp.dao.UserDao;
import com.star.starp.entity.user.User;
import com.star.starp.service.intf.UserService;
import com.star.starp.utils.StringUtils;
import com.star.starp.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/***
 * @className: UserServiceImpl
 * @author: Alex
 * @date: 2022/1/2 19:10
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao dao;
    @Override
    public R checkPhone(String phone) {
        if (StringUtils.isNotEmpty(phone)&&phone.matches(SystemConfig.REG_PHONE)){
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("phone",phone);
            if (dao.selectOne(wrapper) == null){
                return R.ok();
            }
            return R.fail("此手机号已注册");

        }
        return R.fail("非法请求");
    }

    @Override
    public R checkUserName(String username) {

        if (StringUtils.isNotEmpty(username)){
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("username",username);
            if (dao.selectOne(wrapper) == null){
                return R.ok();
            }
            return R.fail("此账号已存在");

        }
        return R.fail("非法请求");
    }
}
